var events = require('events');
var util = require('util');

var DataStore = module.exports = function(cfg, callback) {
	var NDEBUG = cfg['ndebug'];
	
	var type = cfg['type'];
	if (!type) throw new Error('Data store configuration "type" parameter required');
	if ((type.indexOf('.')>=0) || (type.indexOf('/')>=0) || (type.indexOf('\\')>=0)) {
		throw new Error('Data store configuration "type" parameter can not contain dots (.), slashes (/) or backslashes (\\)');
	}
	var StoreImpl = require('./stores/store-'+type+'.js');
	events.EventEmitter.call(this);
	
	this.save = function (data, timeOffset, serverTime, clientTime) {
		this.emit('save', data, timeOffset, serverTime, clientTime);
		//process.nextTick(this.emit('save', data, timeOffset, serverTime, clientTime));
		return '1'; //confirm receiving information even if there will be error in saving it to DB
	}

	StoreImpl(cfg);
	
	//attach as event listener
	this.on('save', StoreImpl.save);

	process.nextTick(callback);
};
util.inherits(DataStore, events.EventEmitter);
